package com.yilin.tms.data.controller;

import com.yilin.commons.util.ArrayUtil;
import com.yilin.commons.util.MapUtil;
import com.yilin.commons.util.PdfUtil;
import com.yilin.tms.core.application.AppUtil;
import com.yilin.tms.core.application.PlatRequest;
import com.yilin.tms.core.application.ReturnData;
import com.yilin.tms.core.commons.base.BaseController;
import com.yilin.tms.core.commons.annotation.NotNull;
import com.yilin.tms.core.commons.utils.ParamUtil;
import com.yilin.tms.core.components.minio.FileDealUtil;
import com.yilin.tms.core.components.minio.MinIoUploadResDTO;
import com.yilin.tms.core.entity.data.receipt.ReceiptOrder;
import com.yilin.tms.core.entity.order.transit.TransitOrder;
import com.yilin.tms.core.entity.order.transit.TransitOrderNote;
import com.yilin.tms.data.service.IMinOssService;
import com.yilin.tms.data.service.IReceiptOrderService;
import com.yilin.tms.data.service.impl.MinOssService;
import com.yilin.tms.core.remote.IOrderRemote;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import java.io.File;
import java.util.HashMap;
import java.util.Map;

/**
 * 电子签收单控制器
 *
 * @author zhangjinyuan
 * @date 2022/11/1
 */
@RestController
@RequestMapping("/data/receiptOrder/")
public class ReceiptOrderController extends BaseController {
    @Value("${servlet.temporary-path}")
    String temporaryPath;
    
    @Resource
    IOrderRemote orderRemote;
    @Resource
    IReceiptOrderService receiptService;
    @Resource
    private IMinOssService minOssService;

    /**
     * 生成电子回单
     */
    @RequestMapping(value = "saveElcReceiptOrder")
    public ReturnData<Object> saveElcReceiptOrder(TransitOrderNote transitOrderNote) {
        ReceiptOrder receiptOrder = receiptService.getSaveElcReceiptOrderByNoteId(transitOrderNote.getId());
        return jsonView(true,"生成电子回单成功",receiptOrder);
    }

    /**
     * 生成电子回单下载
     * */
    @PostMapping("genReceiptPdf")
    @NotNull({"transitOrderId"})
    public ReturnData<Object> genReceiptPdf(String transitOrderNoteId)  {
        ReceiptOrder receiptOrder = receiptService.getElcReceiptOrderByNoteId(transitOrderNoteId);
        String html = PlatRequest.selfGet(getLoginUser().getToken(), "/data/views/elcReceiptOrder", MapUtil.generate(map -> {
            map.put("transitOrderNoteId", transitOrderNoteId);
            map.put("pdfPrint", true);
        }));
        String filePath = AppUtil.getTemporaryPath(temporaryPath)+"Receipt\\";
        if (!new File(filePath).exists()) new File(filePath).mkdir();
        String pdfPath = String.format("%s%s.pdf", filePath, receiptOrder.getTransportNo()+"_"+receiptOrder.getReceiptNo());
        boolean success = PdfUtil.html2Pdf(ArrayUtil.initList(html),pdfPath);
        if (!success) return jsonView(false,"生成回单PDF失败！请重试！");
        MultipartFile multipartFile = FileDealUtil.getMultipartFile(new File(pdfPath));
        MinIoUploadResDTO upload = minOssService.upload(multipartFile, MinOssService.UploadType.temp.path, "receipt");
        Map<String, String> retMap = new HashMap<>();
        retMap.put("fileName", receiptOrder.getTransportNo()+"_"+receiptOrder.getReceiptNo());
        retMap.put("fileUrl", upload.getMinFileUrl());
        return jsonView(true, "临时路径生成成功，请尽快下载！", retMap);
    }

}
